<?php

namespace app\controller;

use app\BaseController;
use app\model\User;
use think\App;
use think\facade\Db;

class TestUser extends BaseController
 {

  public function index(){
//      $user = \app\model\User::select();
//      $user = User::select();
//      return json($user);
//     $user = Db::name('user')->select();
//      $user=User::find(1);
//      echo $user->status;
//      echo  $user->getData('status');
//  return json($user);

      $user=User::select()->withAttr('age',function($age){
          return $age + 100;
      });

      return json($user);
  }

  public function add(){

      $user = new User();
//      $user->name = '李白';
//      $user->age=50;
//      $user->gender='男';
//      $user->details='我可是李白';
//      $user->save();


//      $user->save([
//          'name'=>'杜甫',
//          'age'=>45,
//          'gender'=>'男',
//          "details" =>  "一行白鹭上青天，好诗！"
//      ]);

//      $user->allowField(['name','age','gender'])->save([
//          'age'=>18,
//          'name'=>'李逍遥',
//          "gender"=>'男',
//          'details'=>'剑三'
//      ]);

//      $user->replace()->save([
//          'id'=>20,
//          'name'=>'龙葵1',
//          'age'=>18,
//          'gender'=>'女',
//          'details'=>'刘诗诗'
//      ]);
//
//      return $user->id;

//      return $user->saveAll([
//          [
//              "name"  =>  "赵六",
//              "age"   =>  19,
//              "gender"=>  "男"
//          ],
//          [
//              "name"  =>  "钱七",
//              "age"   =>  22,
//              "gender"=>  "男",
//              "details"   =>  "我很有钱，排行老七！"
//          ]
//      ]);

      $user=User::create([
          "name"  =>  "酒剑仙",
          "age"   =>  58,
          "gender"=>  "男",
          "details"   =>  "我是隐藏主角！"
      ], ["name", "age", "gender", "details"],false);

      return $user->name;
  }

    public function delModel()
    {
//        $userId = User::find(17);
//        return $userId->delete();

//        return User::destroy([21,22]);

//        return User::where('id','>',20)->delete();

//        闭包删除
//        return User::destroy(function ($query){
//            $query->where('id','>',20)->delete();
//        });

//        软删除
//        echo User::destroy(36);

       echo User::destroy(20);

  }


    public function updateFn()
    {
//        $user=User::find(20);
//         $user->name='龙葵';
//         return $user->save();

//        $user=User::where('name','李逍遥')->find();
//        $user->age=Db::raw('age+2');
//        $user->details='我是剑三主角';
//       echo $user->force()->save();

//        $user= new User();
//        $user->saveAll([
//            ['id'=>17,'details'=>'批量更改'],
//            ['id'=>18,'details'=>'批量更改'],
//            ['id'=>19,'details'=>'批量更改'],
//        ]);
//        echo $user->name;

        return User::update(['gender'=>'女','name'=>'贪吃鬼'],['id'=>18],['gender']);
  }

  public function findFn()
  {
//      $user = User::find(18);
//      $user=User::select();
//      $user=User::where('id','>',18)->select();
//      $user =User::count();
//      $user=User::limit(3)->order('id','desc')->select();
      $user=User::max('age');
      return json($user);
  }

//  配合搜索器搜索数据
    public function searchWith()
    {
        $user=User::withSearch(
            ['name','create_time'],[
                "name"=>'李',
                'create_time'=>['1997-1-1',"2024-12-12"],
                'sort'=>['id'=>'desc']
            ]
        )->select();
//        return  Db::getLastSql();
        return json($user);
}

}